package com.wn.common.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import com.wn.common.databinding.DialogPhotoChooseBinding
import com.wn.framework.base.BaseDialog
import com.wn.framework.base.BaseDialogFragment
import com.wn.framework.ext.onClick

/**
 * @Author: wn
 * @Date: 2025/7/30 15:33
 * @Description: 拍照、相册选择弹框
 */
class ChoosePhotoDialog {
  class Builder(activity: FragmentActivity) : BaseDialogFragment.Builder<Builder>(activity) {
    private var mOnTakePicturesCall: (() -> Unit)? = null
    private var mOnPhotoAlbumCall: (() -> Unit)? = null

    /**
     * 此处为实例 的 XML 布局
     */
    private val mBinding: DialogPhotoChooseBinding =
      DialogPhotoChooseBinding.inflate(LayoutInflater.from(activity))

    init {
      initView()
    }

    private fun initView() {
      // 绑定 实例 布局
      setContentView(mBinding.root)
      // 设置宽高
      setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
      setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
      // 设置对话框 弹出 动画样式
      setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
      // 修改 Dialog 重心 为 底部
      gravity = Gravity.BOTTOM

      mBinding.apply {
        // 打开相册 -- 触发传递的 lambda
        clPhotoAlbum.onClick {
          mOnPhotoAlbumCall?.invoke()
          dismiss()
        }
        // 打开相机 -- 触发传递的 lambda
        clTakePictures.onClick {
          mOnTakePicturesCall?.invoke()
          dismiss()
        }
        // 取消
        tvCancel.onClick {
          dismiss()
        }
      }
    }

    /**
     * 传递打开相机的回调 lambda
     */
    fun setTakePicturesCall(onTakePicturesCall: (() -> Unit)): Builder {
      mOnTakePicturesCall = onTakePicturesCall
      return this
    }

    /**
     * 传递打开相册的回调 lambda
     */
    fun setPhotoAlbumCall(onPhotoAlbumCall: (() -> Unit)): Builder {
      mOnPhotoAlbumCall = onPhotoAlbumCall
      return this
    }
  }
}